/*
 * AddExistingPIDMFrame.java
 *
 * --- Last Update: 6/19/2010 6:51 PM ---
 *
 * Update Notes 6/19/2010 6:51 PM by Adrian Wijasa:
 * Removed all uses of main.checkConnection methods. Before, the checkConnection method is used to let the
 * user enters his/her credentials whenever any info needs to be obtained from Database. Now, users have to
 * provide their credentials before AddExistingPIDMFrame is opened.
 *
 * Update Notes 4/23/2010 5:49 PM by Adrian Wijasa:
 * Now uses ExistingPIDMQuery.ID instead of ExistingPIDMQuery.ALUM_ID.
 *
 * Update Notes 4/22/2010 9:18 PM by Adrian Wijasa:
 * Changed the wordings to avoid confusion when this program is used by other colleges:
 * 1. ALUM ID to just ID
 * 2. 900 ID to 900 ID (CSBSJU ID Only)
 *
 * Update Notes 4/17/2010 11:28 AM by Adrian Wijasa:
 * This version is made to work with Java 5.
 *
 * Update Notes 3/22/2010 4:48 PM by Adrian Wijasa:
 * Re-adjusted the form design so that it will look good under Java Metal UI.
 *
 * Created on April 30, 2007, 9:15 AM
 *
 * CSV Loader
 * Copyright 2007, 2009, 2010 Adrian Wijasa
 *
 * This file is part of CSV Loader.
 *
 * CSV Loader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CSV Loader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CSV Loader.  If not, see <http://www.gnu.org/licenses/>.
 */

package forms;

import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.ListSelectionModel;
import sql.ExistingPIDMQuery;
import table.StaticTableModel;

/**
 *
 * @author matianyuan
 */
public class AddExistingPIDMFrame extends javax.swing.JFrame {

    /** Creates new form AddExistingPIDMFrame5 */
    public AddExistingPIDMFrame( Main main ) {
        this.main = main;
        cancel = true;
        initComponents();
        initRadioButtons();
        initTable();
        setLocation( main.getX(), main.getY() + 170 );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        introLabel = new javax.swing.JLabel();
        ssnRadioButton = new javax.swing.JRadioButton();
        idRadioButton = new javax.swing.JRadioButton();
        prodIDRadioButton = new javax.swing.JRadioButton();
        colSelectorLabel = new javax.swing.JLabel();
        colLabel = new javax.swing.JLabel();
        colonLabel = new javax.swing.JLabel();
        colTableScrollPane = new javax.swing.JScrollPane();
        colTable = new javax.swing.JTable();
        okLabel = new javax.swing.JLabel();
        cancelLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Existing PIDM");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        introLabel.setText("How to obtain the PIDM information?");

        ssnRadioButton.setText("Through SSN");
        ssnRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        ssnRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ssnRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ssnRadioButtonActionPerformed(evt);
            }
        });

        idRadioButton.setText("Through ID");
        idRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        idRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        idRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idRadioButtonActionPerformed(evt);
            }
        });

        prodIDRadioButton.setText("Through 900 ID (CSBSJU ID Only)");
        prodIDRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        prodIDRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        prodIDRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prodIDRadioButtonActionPerformed(evt);
            }
        });

        colSelectorLabel.setText("Select the column that contains ");

        colLabel.setText("SSN");

        colonLabel.setText(":");

        colTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        colTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        colTable.setColumnSelectionAllowed(true);
        colTableScrollPane.setViewportView(colTable);

        okLabel.setFont(new java.awt.Font("Dialog", 1, 11)); // NOI18N
        okLabel.setForeground(new java.awt.Color(0, 102, 255));
        okLabel.setText("OK");
        okLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                okLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                okLabelMouseExited(evt);
            }
        });

        cancelLabel.setFont(new java.awt.Font("Dialog", 1, 11)); // NOI18N
        cancelLabel.setForeground(new java.awt.Color(0, 102, 255));
        cancelLabel.setText("Cancel");
        cancelLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cancelLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                cancelLabelMouseExited(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(colTableScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 781, Short.MAX_VALUE)
                            .add(introLabel)
                            .add(ssnRadioButton)
                            .add(idRadioButton)
                            .add(prodIDRadioButton)
                            .add(layout.createSequentialGroup()
                                .add(colSelectorLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(colLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(colonLabel))))
                    .add(layout.createSequentialGroup()
                        .add(340, 340, 340)
                        .add(okLabel)
                        .add(83, 83, 83)
                        .add(cancelLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                        .add(316, 316, 316)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(introLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(ssnRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(idRadioButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(prodIDRadioButton)
                .add(21, 21, 21)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(colSelectorLabel)
                    .add(colLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(colonLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(colTableScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                .add(10, 10, 10)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelLabel)
                    .add(okLabel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ssnRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ssnRadioButtonActionPerformed
        colLabel.setText( "SSN" );
}//GEN-LAST:event_ssnRadioButtonActionPerformed

    private void idRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idRadioButtonActionPerformed
        colLabel.setText( "ID" );
}//GEN-LAST:event_idRadioButtonActionPerformed

    private void prodIDRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prodIDRadioButtonActionPerformed
        colLabel.setText( "900 ID" );
}//GEN-LAST:event_prodIDRadioButtonActionPerformed

    private void okLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okLabelMouseClicked
        okLabel.setForeground( main.red );
        cancel = false;

        if( ssnRadioButton.isSelected() )
            main.addExistingPIDMCol( colTable.getSelectedColumn(), ExistingPIDMQuery.SSN );
        else if( idRadioButton.isSelected() )
            main.addExistingPIDMCol( colTable.getSelectedColumn(), ExistingPIDMQuery.ID );
        else
            main.addExistingPIDMCol( colTable.getSelectedColumn(), ExistingPIDMQuery.PROD_ID );

        dispose();
}//GEN-LAST:event_okLabelMouseClicked

    private void okLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okLabelMouseExited
        okLabel.setForeground( main.blue );
}//GEN-LAST:event_okLabelMouseExited

    private void okLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okLabelMouseEntered
        okLabel.setForeground( main.black );
}//GEN-LAST:event_okLabelMouseEntered

    private void cancelLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelLabelMouseClicked
        cancelLabel.setForeground( main.red );
        dispose();
}//GEN-LAST:event_cancelLabelMouseClicked

    private void cancelLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelLabelMouseExited
        cancelLabel.setForeground( main.blue );
}//GEN-LAST:event_cancelLabelMouseExited

    private void cancelLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelLabelMouseEntered
        cancelLabel.setForeground( main.black );
}//GEN-LAST:event_cancelLabelMouseEntered

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        if( cancel ) {
            main.setEnabled( true );
            main.requestFocus();
        }
    }//GEN-LAST:event_formWindowClosed

    private void initRadioButtons() {
        buttonGroup = new ButtonGroup();
        buttonGroup.add( ssnRadioButton );
        buttonGroup.add( idRadioButton );
        buttonGroup.add( prodIDRadioButton );

        ssnRadioButton.setSelected( true );
    }

    private void initTable() {
        colTable.getTableHeader().setReorderingAllowed( false );
        colTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        colTable.setModel( new StaticTableModel( main.get2DVector( main.csvPanel.currentImage.csvDataArrayList ), new Vector<String>( main.csvPanel.currentImage.columnArrayList ) ) );
        colTable.setRowSelectionInterval( 0, 0 );
        colTable.setColumnSelectionInterval( 0, 0 );
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cancelLabel;
    private javax.swing.JLabel colLabel;
    private javax.swing.JLabel colSelectorLabel;
    private javax.swing.JTable colTable;
    private javax.swing.JScrollPane colTableScrollPane;
    private javax.swing.JLabel colonLabel;
    private javax.swing.JRadioButton idRadioButton;
    private javax.swing.JLabel introLabel;
    private javax.swing.JLabel okLabel;
    private javax.swing.JRadioButton prodIDRadioButton;
    private javax.swing.JRadioButton ssnRadioButton;
    // End of variables declaration//GEN-END:variables

    private boolean cancel;
    private ButtonGroup buttonGroup;
    private ExistingPIDMQuery pidmQuery;
    private Main main;
}
